<?php
namespace lib;
/**
 * dbo数据库连接类
 */
class db
{
    public static $_i = null;
    public static $pdh = null;
    public $ret;
    public $issel=true;
    public static $db_conf;

    static function i($db_conf = array())
    {
        if (is_null(self::$_i))
        {
            self::$_i = new db();
        }
        self::$_i or self::$_i=new \lib\db();
        if (self::$pdh)
        {
            return self::$_i;
        }
        if (
            empty($db_conf['dbname'])
            || empty($db_conf['dbhost'])
            || empty($db_conf['dbport'])
            || empty($db_conf['dbuser'])
            || empty($db_conf['dbpwd'])
        )
        {
            var_dump("config error", "20131014150033");
            exit();
        }

        try
        {
            self::$pdh = new \PDO('mysql:host=' . $db_conf['dbhost'] . ';port=' . $db_conf['dbport'] . ';dbname=' . $db_conf['dbname'],
                $db_conf['dbuser'],
                $db_conf['dbpwd']
            );
            self::$pdh->query('set names utf8');
            self::$pdh->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
            self::$pdh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
            self::$db_conf = $db_conf;
        } catch (PDOException $e)
        {
            var_dump($e->getMessage(), "20131014150847");
        }

        return self::$_i;
    }

    public function presql($pars = array(), $reset = true)
    {
        $reset and $this->ret = null;
        foreach ($pars as $k => $par_stu)
        {
            $this->issel=true;
            stripos(trim($par_stu['sql']),'select')===0 or $this->issel=false;
            $stmt = self::$pdh->prepare($par_stu['sql']);

            if (empty($par_stu['pars']))
            {
                $stmt->execute();
                $this->ret[$k][] = $stmt->fetchAll();
            }
            else
            {
                $i = 0;
                foreach ($par_stu['pars'] as $par_k => $par_emt)
                {
                    if ($i == 0)
                    {
                        foreach ($par_emt as $emt_k => $emt_v)
                        {
                            $stmt->bindParam(":" . $emt_k, $$emt_k);
                        }
                        $i++;
                    }

                    foreach ($par_emt as $emt_k => $emt_v)
                    {
                        $$emt_k = $emt_v;
                    }

                    try
                    {
                        isset($this->ret['_pdo_']['rowCount']) or $this->ret['_pdo_']['rowCount']=0;
                        $exec_ret                           = $stmt->execute();
                        $this->ret['_pdo_']['lastInsertId'] = self::$pdh->lastInsertId();
                        $this->ret['_pdo_']['rowCount']    += $stmt->rowCount();
                        $this->ret['_pdo_']['errorInfo']    = self::$pdh->errorInfo();

                        $this->issel and $this->ret[$k][$par_k]              = $stmt->fetchAll();
                    } catch (Exception $e)
                    { //如果fetchAll执行错误,就将execute的值赋给ret.
                        $this->ret[$k][$par_k]['exec_ret'] = isset($exec_ret)?$exec_ret:'no exec_ret';
                        $this->ret[$k][$par_k]['e']        = $e;
                    }
                }
            }

        }

        return $this->ret;
    }
}
